<%@page import="sql.SearchUtility"%>
<%@page import="sql.DBManagment"%>
<%@page import="sql.record.*"%>
<%@page import="java.util.*"%>
<%
String searchQuery = request.getParameter("AJAXsearchBox");
List<String> keywords = new LinkedList<String>();
for(String s : searchQuery.split(" ")){
	keywords.add(s);
}

String username = (String) session.getAttribute("loggedUser");
List<TableRecord> courseSchedList = SearchUtility.searchDB(username,keywords);

//will get all courses
List<Course> courses = new LinkedList<Course>();
for(TableRecord t: courseSchedList){
	if(t instanceof Course){
		int lastIndex = courses.size() - 1;
		Course currentCourse = (Course)t;
		if((lastIndex == -1) || (!courses.get(lastIndex).courseName.equals(currentCourse.courseName))){
			courses.add((Course)t);
		}
	}
}
			for(Course c: courses){
		%>		
			<div class="panel" align="justify">
			<span class="orangetitle">
				<input type="checkbox" name="mark" id="mark" value=<%="\"" + c.courseName + "\""%> />
		      	<%=c.courseName%>
		      	<input type="hidden" value=<%="\"" + c.courseName + "\""%> class="hiddenData" />
		    </span>
			<div class="bodytext"><br />
				<ul>
					<li><b>Course Name: </b><%=c.courseName%></li>
					<li>
						<b>Credit Points: </b><%=c.creditPoints%>
						<input type="hidden" value=<%="\"" + c.creditPoints + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Capacity: </b><%=c.capacity%>
						<input type="hidden" value=<%="\"" + c.capacity + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Description: </b><br/><%=c.description%>
						<input type="hidden" value=<%="\"" + c.description + "\""%> class="hiddenData" />
					</li>
				</ul>
				<strong>Schedule</strong>
				<ul>
				<%
					String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday"};
					for(TableRecord t : courseSchedList){
						if((t instanceof CourseSchedule) && (((CourseSchedule)t).courseName.equals(c.courseName))){
						CourseSchedule currentSched = ((CourseSchedule)t);
					%>
						<%
							String startTime = currentSched.startHour.toString();
							String endTime = currentSched.endHour.toString();
							if(currentSched.startHour.intValue() < 1000){
								startTime = "0" + startTime;
							}
							if(currentSched.endHour.intValue() < 1000){
								endTime = "0" + endTime;
							}							
						%>
						<li><%=days[currentSched.day]%>, <%=startTime%> to <%=endTime%> </li>
					<%		
						}
					}
				%>
				</ul>
		    </div>
		</div>
		<%
			}
		%>